<script lang="ts" setup>
import type { Button } from "@/common/types/button"

interface Props {
  title: string
  subtitle?: string
  buttons?: Button[]
  size?: "large" | "medium" | "small"
}

const props = withDefaults(defineProps<Props>(), {
  buttons: () => [],
  size: "large"
})

// 处理按钮类型，将自定义类型映射到Element Plus支持的类型
function getButtonType(type: string | undefined): "primary" | "success" | "warning" | "danger" | "info" | "default" {
  if (!type) return "default"
  // 自定义类型使用默认样式，通过CSS类名来区分
  if (["paid-user", "trial-active", "trial-expired", "version-standard", "version-pro", "version-pro-max"].includes(type)) {
    return "default"
  }
  return type as "primary" | "success" | "warning" | "danger" | "info" | "default"
}
</script>

<template>
  <div class="page-header">
    <div class="header-left">
      <h1 class="title" :class="`title--${props.size}`">{{ props.title }}</h1>
      <p v-if="props.subtitle" class="subtitle" :class="`subtitle--${props.size}`">{{ props.subtitle }}</p>
    </div>
    <div class="header-right">
      <!-- 右侧可插入自定义区域（如下拉选择等） -->
      <div class="extra">
        <slot name="extra" />
      </div>
      <el-button
        v-for="(button, index) in props.buttons"
        :key="index"
        :type="getButtonType(button.type)"
        :class="button.type"
        @click="button.onClick"
      >
        <el-icon v-if="button.icon" class="mr-1">
          <Plus v-if="button.icon === 'Plus'" />
          <Edit v-else-if="button.icon === 'Edit'" />
          <component v-else :is="button.icon" />
        </el-icon>
        {{ button.text }}
      </el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 24px 0;
  border-bottom: 1px solid var(--el-border-color-lighter); /* 跟随主题的边框主题色 */
  margin-bottom: 24px;

  .header-left {
    .title {
      font-weight: 600;
      color: var(--el-text-color-primary); /* 跟随主题的字体主题色 */
      margin: 0;
      line-height: 1.2;

      // 大尺寸 - 默认页面标题
      &--large {
        font-size: 24px;
      }

      // 中等尺寸 - 弹窗或小区域标题
      &--medium {
        font-size: 18px;
      }

      // 小尺寸 - 紧凑区域标题
      &--small {
        font-size: 16px;
      }
    }

    .subtitle {
      color: var(--el-text-color-secondary); /* 跟随主题的次要字体主题色 */
      margin: 4px 0 0 0;
      line-height: 1.4;

      // 大尺寸副标题
      &--large {
        font-size: 14px;
      }

      // 中等尺寸副标题
      &--medium {
        font-size: 13px;
      }

      // 小尺寸副标题
      &--small {
        font-size: 12px;
      }
    }
  }

  .header-right {
    display: flex;
    gap: 12px;
    align-items: center;

    .extra {
      display: flex;
      gap: 8px;
      align-items: center;

      :deep(.el-select) {
        min-width: 120px;
        .el-select__wrapper {
          border-radius: 6px;
          border: 0.5px solid #e5e7eb;
          height: 36px;
        }
      }
    }

    .el-button {
      display: flex;
      align-items: center;
      padding: 8px 16px;
      font-size: 14px;
      border-radius: 6px;
      font-weight: 500;

      .mr-1 {
        margin-right: 8px;
        font-size: 16px;
      }

      &.el-button--primary {
        color: white;

        .el-icon {
          color: white;
        }
      }
    }
  }
}
</style>
